package com.be.beadmin.device.controller;

import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.device.entity.DeviceInfo;
import com.be.beadmin.device.dto.output.DeviceInfoEx;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;

import com.be.beadmin.device.service.DeviceInfoService;
import com.be.beadmin.device.dto.input.DeviceInfoQueryCriteria;
import com.be.beadmin.device.dto.input.DeviceInfoDto;
import org.springframework.data.domain.Pageable;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.util.List;
import com.alibaba.excel.EasyExcel;

/**
* @author chenxu
* @date 2023-09-07
**/
@RestController
public class DeviceInfoController {

    @Autowired
    private  DeviceInfoService deviceInfoService;


    @PostMapping(value = "/deviceinfo/getDeInfoByPage")
    public ResultModel getDeviceInfoByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(deviceInfoService.getDeviceInfoByPage(pageEntity)) ;
    }

    @PostMapping("/deviceinfo/addDeInfo")
    public ResultModel addDeviceInfo(@RequestBody DeviceInfoDto deviceInfoDto){
        deviceInfoService.addDeviceInfo(deviceInfoDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/deviceinfo/delDeInfo")
    public ResultModel delDeviceInfo(@RequestBody DeviceInfoDto deviceInfoDto){
        deviceInfoService.delDeviceInfo(deviceInfoDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/deviceinfo/updateDeInfo")
    public ResultModel updateDeviceInfo(@RequestBody DeviceInfoDto deviceInfoDto){
        deviceInfoService.updateDeviceInfo(deviceInfoDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/deviceinfo/exportDeInfo")
    public void exportDeviceInfo(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<DeviceInfoEx> deviceInfoPage = deviceInfoService.getDeviceInfoByPage(pageEntity);
        List<DeviceInfoEx> deviceInfoExes = deviceInfoPage.getContent();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("DeviceInfo接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        //EasyExcel.write(response.getOutputStream(), DeviceInfoEx.class).sheet("sheet1").doWrite(deviceInfoExes);
        ExcelWriterBuilder ewb=EasyExcel.write(response.getOutputStream(), DeviceInfoEx.class);
        ewb.excelType(ExcelTypeEnum.XLS);
        ewb.sheet("sheet1").doWrite(deviceInfoExes);
    }

    @ApiOperation("获取所有设备编号")
    @PostMapping("/device/getDeviceIds")
    public ResultModel getDeviceIds(){
        return ResultModel.success(deviceInfoService.getDeviceIds());
    }
}